package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;
import com.telecom.cms.dto.*;

import com.telecom.cms.util.CurrentTime;

/**
 * @author yueyingnishang
 * 
 */
public class CollectionAction11 extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String apptaget = request.getParameter("apptagat");
		String appname =  request.getParameter("appname");
		String appuser =  request.getParameter("appuser");
		String appphone = request.getParameter("appphone");
		String apptime  = request.getParameter("apptime");

		String latn_id = (String) request.getSession().getAttribute("oldid");

		String nowtime = CurrentTime.getCurrentTime();

		Connection conn = GetConnect.getCsConnection();

		conn.setAutoCommit(false);
		
		//CUBE申请表插入
		
		String insertsql1 = "insert into " + Constant.SEG_APPLY_CUBE_DEF + "   values(,'"+appname+"','"+apptaget+"','1','"
		                          +appuser+"','"+appphone+"','"+apptime+"','"+nowtime+"','"+latn_id+"',)";

		PreparedStatement ps1 = conn.prepareStatement(insertsql1);
		ps1.executeUpdate();
		if (ps1 != null) {

			ps1.close();
		}
	
       //主题表插入 
		String insertsql2 = " insert into "
				+ Constant.SEG_APP_SUBJECT_DEF
				+ "(APP_SUBJECT_ID,APP_SUBJECT_NAME,APP_SUBJECT_DESC ,ANLS_CLASS_ID,FOLEDER_ID,JOINT_MODE_ID,USER_ID,CREATE_TIME, LATN_ID)"
				+ " values( ,'" + appname + "','','',,'','',,"+Integer.parseInt(latn_id)+")";

		PreparedStatement ps2 = conn.prepareStatement(insertsql2);
		ps2.executeUpdate();
		if (ps2 != null) {

			ps2.close();

		}
		// end
		// 分群规则定义表表插入
		// 先取主题表的主题ID
		System.out.println(" select APP_SUBJECT_ID from "
				+ Constant.SEG_APP_SUBJECT_DEF + " where APP_SUBJECT_NAME = "
				+ appname);
		
		
		PreparedStatement psmt2 = conn
				.prepareStatement(" select APP_SUBJECT_ID from "
						+ Constant.SEG_APP_SUBJECT_DEF + " where APP_SUBJECT_NAME = '"
						+ appname+"'");
		System.out.println(" select APP_SUBJECT_ID from "
				+ Constant.SEG_APP_SUBJECT_DEF + " where APP_SUBJECT_NAME = "
				+ appname);
		
		ResultSet rs2 = psmt2.executeQuery();
		int APP_SUBJECT_ID = 0;
		if (rs2.next()) {

			APP_SUBJECT_ID = rs2.getInt(1);

		}
		if (rs2 != null) {

			rs2.close();
		}
		if (psmt2 != null) {

			psmt2.close();
		}

		List atomlist = (List) request.getSession().getAttribute("AtomList");

		PreparedStatement psmt3 = null;

		for (int i = 0; i < atomlist.size(); i++) {

			ShowatomDTO dto = (ShowatomDTO) atomlist.get(i);

			String insertsql3 = "insert into "
					+ Constant.SEG_SEG_RULE_DEF
					+ "(RULE_ID,RULE_KIND_ID,APP_SUBJECT_ID, BIZ_ATOM_ID, BIZ_ATOM_NAME,VAULE_ONE,VALUE_TWO,VALUE_TYPE_ID,RST_CDN_ID,RST_CDN_NAME,RST_CDN_TYPE)"
					+ " values( , ," + APP_SUBJECT_ID + "," + dto.getAtomid()
					+ ",'" + dto.getAtomname() + "','"
					+ dto.getAtomvalue().replaceAll("'", "''") + "','"
					+ dto.getChinesevalue().replaceAll("'", "''")
					+ "','','','','')";

			psmt3 = conn.prepareStatement(insertsql3);

			psmt3.executeUpdate();
		}

		if (psmt3 != null) {

			psmt3.close();
		}

		// end
		// 分群规则展示结果定义表插入
		List outputlist = (List) request.getSession()
				.getAttribute("Outputlist");
		PreparedStatement psmt4 = null;

		if (outputlist != null) {

			for (int j = 0; j < outputlist.size(); j++) {

				SEG_BIZ_ATOM_DEFDTO dto = (SEG_BIZ_ATOM_DEFDTO) outputlist
						.get(j);

				String insertsql4 = " insert into "
						+ Constant.SEG_SEG_RULE_SHOWATOM
						+ "(BIZ_ATOM_ID,APP_SUBJECT_ID, BIZ_ATOM_NAME, RULE_KIND_ID) values("
						+ dto.getBIZ_ATOM_ID() + "," + APP_SUBJECT_ID + ",'"
						+ dto.getBIZ_ATOM_NAME() + "',)";
				psmt4 = conn.prepareStatement(insertsql4);

				psmt4.executeUpdate();

			}

			if (psmt4 != null) {

				psmt4.close();
			}
		}
		// end
		try {
			conn.commit();

		} catch (Exception e) {

			conn.rollback();

		} finally {

			conn.setAutoCommit(true);

		}

		return mapping.findForward("success");
	}

}
